package adminuser

import (
	"context"

	"gitee.com/liuxuezhan/ar-platform/admin-api/internal/svc"
	"gitee.com/liuxuezhan/ar-platform/admin-api/internal/types"

	"gitee.com/liuxuezhan/ar-platform/rpc-client/userServiceClient/userrpc"
	"github.com/zeromicro/go-zero/core/logc"
	"github.com/zeromicro/go-zero/core/logx"

	v1 "gitee.com/liuxuezhan/ar-platform/rpc-client/authRoleClient/api/role/v1"
)

type AdminUserSearchLogic struct {
	logx.Logger
	ctx    context.Context
	svcCtx *svc.ServiceContext
}

// 根据条件查询管理员用户
func NewAdminUserSearchLogic(ctx context.Context, svcCtx *svc.ServiceContext) *AdminUserSearchLogic {
	return &AdminUserSearchLogic{
		Logger: logx.WithContext(ctx),
		ctx:    ctx,
		svcCtx: svcCtx,
	}
}

func (l *AdminUserSearchLogic) AdminUserSearch(req *types.AdminUserSearchRequest) (resp *types.AdminUserSearchResponse, err error) {
	resp = &types.AdminUserSearchResponse{}
	user_list := make([]*types.AdminUserInfo, 0)

	ids := make([]int64, 0)

	if req.Role != 0 {
		// 首先调 roles
		getRolesAccountIdsResp, err := l.svcCtx.AuthRoleRpc.GetRolesAccountIds(l.ctx, &v1.GetRolesAccountIdsRequest{
			RoleIds:     []int64{int64(req.Role)},
			AccountType: v1.AccountType_User,
		})
		if err != nil {
			logc.Error(l.ctx, "GetRolesAccountIds err: ", err)
			return nil, err
		}
		// getAccountRolesResp, err := l.svcCtx.AuthRoleRpc.GetAccountRoles(l.ctx, &v1.GetAccountRolesRequest{})

		if len(getRolesAccountIdsResp.AccountIds) == 0 {
			return resp, nil
		}
		ids = getRolesAccountIdsResp.AccountIds
	}

	// 然后拿 roles 返回的 ids 调接口
	adminUserListByIdsResp, err := l.svcCtx.UserServiceRPC.AdminUserListByIds(l.ctx, &userrpc.AdminUserListByIdsRequest{
		AccountKeyword: req.AccountKeyword,
		IdList:         ids,
		UserStatus:     req.UserStatus,
		LoginStatus:    req.LoginStatus,
		SortKeyword:    req.SortKeyword,
		Page:           req.Page,
		PageSize:       req.PageSize,
	})
	if err != nil {
		logc.Error(l.ctx, "AdminUserListByIds err: ", err)
		return nil, err
	}

	roleListResp, err := l.svcCtx.AuthRoleRpc.GetRoleList(l.ctx, &v1.GetRoleListRequest{
		Page:     0,
		PageSize: 100,
		Status:   1,
	})
	if err != nil {
		logc.Error(l.ctx, "GetRoleList err: ", err)
		return nil, err
	}

	for _, user := range adminUserListByIdsResp.AdminUserList {

		user := &types.AdminUserInfo{
			Id:              user.Id,
			Account:         user.Account,
			Password:        user.Password,
			Phone:           user.Phone,
			PhoneCode:       user.PhoneCode,
			Nickname:        user.Nickname,
			Email:           user.Email,
			Role:            user.Role,
			RegisterAt:      user.RegisterAt,
			LoginAt:         user.LoginAt,
			LoginIp:         user.LoginIp,
			LoginNumber:     user.LoginNumber,
			UserStatus:      user.UserStatus,
			LoginStatus:     user.LoginStatus,
			GoogleCode:      user.GoogleCode,
			IsOpenGoogle:    user.IsOpenGoogle,
			IsBindingGoogle: user.IsBindingGoogle,
			HeadPictureUrl:  user.HeadPictureUrl,
		}
		for _, role := range roleListResp.List {
			if role.Id == int64(user.Role) {
				user.RoleName = role.RoleName
			}
		}
		user_list = append(user_list, user)
	}

	resp.AdminUserList = user_list
	resp.Pagination = types.Pagination{
		Page:      adminUserListByIdsResp.Pagination.Page,
		PageSize:  adminUserListByIdsResp.Pagination.PageSize,
		Total:     adminUserListByIdsResp.Pagination.Total,
		TotalPage: adminUserListByIdsResp.Pagination.TotalPage,
	}

	return
}
